﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dami;

public partial class admin_Modules_product_AddNew : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        this.btnCancel.Attributes.Add("onclick","return ClearAll()");

        if (!this.IsPostBack) {
            ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        }
    }

    private Boolean CheckNick(String strNick,DamiWebDataContext db) {
        Boolean bBool = false;
        var query = from p in db.tbl_Nick_Yahoos
                    where p.Nickname == strNick
                    select p;
        if (query.Count<tbl_Nick_Yahoo>() > 0)
            bBool = true;
        return bBool;
    }

    private void AddNew() {
        String strMess = "";
        int id = 0;
            try
            {
                clsFileUpLoad fileUpload = new clsFileUpLoad();
                WebData db = new WebData();
                DataLibrary.tbl_Product product = new DataLibrary.tbl_Product();
                product.IdProduct = 1;
                product.ImageProduct = fileUpload.FileSaveImage("upload/product",this.fulimageProduct);
                product.Path = "";
                product.OrderId = Convert.ToInt32(txtorderId.Text);
                product.IsActive = 1;
                product.Description = txtTieude.Text;
                id = db.tbl_Products.InsertOnSubmit(product);
                product = db.tbl_Products.GetAll()[0];
                if (id > 0) {
                    id = product.IdProduct;
                }

                DataLibrary.tbl_Product_Lang item = new DataLibrary.tbl_Product_Lang();
                item.IdLang = 1;
                item.Description = txtDescription.Text;
                item.IdProduct = id;
                item.IdProductLang = 1;
                item.IntroProduct = txtNoidung.Text;
                item.NameProduct = txtTieude.Text;
                item.Price = Convert.ToDecimal(txtPrice.Text);
                id=db.tbl_Product_Langs.InsertOnSubmit(item);

            }
            catch (Exception err)
            {
                strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
            }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
        if(id>0)
            Response.Redirect(ViewState["URLReferrer"].ToString());

    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
    }

}